package org.pps.gl.client.vue;

import org.pps.gl.client.GetInfoService;
import org.pps.gl.client.GetInfoServiceAsync;
import org.pps.gl.client.Pps_gl;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.TextArea;

/**
 * Affichage des expériences : poste, société, période, domaine d'activité,
 * direction, description
 * 
 */
public class PFL_Experience extends Composite {

	private long idExperience;
	private Profil profil;

	public PFL_Experience(String periode, String nomEntreprise,
			String direction, String poste, String description, String domaine,
			long idExp, Profil p) {

		this.idExperience = idExp;
		this.profil = p;

		VerticalPanel verticalPanel = new VerticalPanel();
		verticalPanel.setStyleName("profilMarge");
		initWidget(verticalPanel);
		verticalPanel.setSize("400px", "130px");

		HorizontalPanel horizontalPanel = new HorizontalPanel();
		verticalPanel.add(horizontalPanel);
		horizontalPanel.setWidth("100%");

		Label lblPoste = new Label(poste);
		horizontalPanel.add(lblPoste);
		lblPoste.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
		lblPoste.setStylePrimaryName("dynTitre");
		lblPoste.setStyleName("dynTitre");
		verticalPanel.setCellVerticalAlignment(lblPoste,
				HasVerticalAlignment.ALIGN_MIDDLE);
		lblPoste.setSize("100%", "30px");
		verticalPanel.setCellHorizontalAlignment(lblPoste,
				HasHorizontalAlignment.ALIGN_CENTER);

		if (p != null) {
			Button sup = new Button("");
			horizontalPanel.add(sup);
			horizontalPanel.setCellHorizontalAlignment(sup,
					HasHorizontalAlignment.ALIGN_RIGHT);
			horizontalPanel.setCellVerticalAlignment(sup,
					HasVerticalAlignment.ALIGN_MIDDLE);
			sup.setStylePrimaryName("boutonSuppression");
			sup.setSize("21px", "21px");
			sup.addClickHandler(new ClickHandler() {
				public void onClick(ClickEvent event) {

					GetInfoServiceAsync infoService = GWT
							.create(GetInfoService.class);
					infoService.deleteExperienceUser(idExperience,
							new AsyncCallback<Boolean>() {

								@Override
								public void onSuccess(Boolean result) {
									// On rafraîchie les données sur la page
									Pps_gl.getCm().refresh();
									profil.afficherExperiences();
									GWT.log("Experience effacée.");
								}

								@Override
								public void onFailure(Throwable caught) {
									GWT.log("Erreur d'effacement d'expérience.");
								}
							});
				}
			});
		}
		
		Label lblNomEntreprise = new Label("à " + nomEntreprise);
		lblNomEntreprise.setStylePrimaryName("dynTitre2");
		lblNomEntreprise.setStyleName("dynTitre2");
		verticalPanel.add(lblNomEntreprise);
		verticalPanel.setCellVerticalAlignment(lblNomEntreprise,
				HasVerticalAlignment.ALIGN_MIDDLE);
		lblNomEntreprise.setSize("100%", "30px");
		verticalPanel.setCellHorizontalAlignment(lblNomEntreprise,
				HasHorizontalAlignment.ALIGN_CENTER);

		Label lblPeriode = new Label(periode);
		lblPeriode.setStylePrimaryName("dynDate");
		lblPeriode.setStyleName("dynDate");
		verticalPanel.add(lblPeriode);
		verticalPanel.setCellVerticalAlignment(lblPeriode,
				HasVerticalAlignment.ALIGN_MIDDLE);
		lblPeriode.setSize("100%", "30px");
		verticalPanel.setCellHorizontalAlignment(lblPeriode,
				HasHorizontalAlignment.ALIGN_CENTER);

		Label lblDirection = new Label("Direction : " + direction);
		lblDirection.setStylePrimaryName("dynTexte");
		lblDirection.setStyleName("dynTexte");
		verticalPanel.add(lblDirection);
		verticalPanel.setCellVerticalAlignment(lblDirection,
				HasVerticalAlignment.ALIGN_MIDDLE);
		lblDirection.setSize("80%", "30px");
		verticalPanel.setCellHorizontalAlignment(lblDirection,
				HasHorizontalAlignment.ALIGN_CENTER);

		Label lblDomaine = new Label("Domaine d'activité : " + domaine);
		lblDomaine.setStylePrimaryName("dynTexte");
		lblDomaine.setStyleName("dynTexte");
		verticalPanel.add(lblDomaine);
		verticalPanel.setCellVerticalAlignment(lblDomaine,
				HasVerticalAlignment.ALIGN_MIDDLE);
		lblDomaine.setSize("80%", "30px");
		verticalPanel.setCellHorizontalAlignment(lblDomaine,
				HasHorizontalAlignment.ALIGN_CENTER);

		TextArea lblDescription = new TextArea();
		lblDescription.setVisibleLines(3);
		lblDescription.setEnabled(false);
		lblDescription.setText(description);
		lblDescription.setStyleName("dynTexte");
		verticalPanel.add(lblDescription);
		verticalPanel.setCellVerticalAlignment(lblDescription,
				HasVerticalAlignment.ALIGN_MIDDLE);
		lblDescription.setSize("80%", "47px");
		verticalPanel.setCellHorizontalAlignment(lblDescription,
				HasHorizontalAlignment.ALIGN_CENTER);

	}

}
